package priv.hxm.jdbcUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import freemarker.cache.StringTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

/*
 * 用来合成模板和数据
 */
public class FreemarkUtil {
	private static Logger logger = Logger.getLogger(FreemarkUtil.class);

	public static void main(String[] args) throws IOException,
			URISyntaxException, TemplateException {
		Map<String, String> dateMap = new HashMap<>();
		String classname = "Memp";
		String packagenameString = "priv/mk/entry".replace("/", ".");
		createEntryClassBody(dateMap, classname, packagenameString);
	}

	public static void createEntryClassBody(Map<String, String> dateMap,
			String classname, String packagename) throws URISyntaxException,
			IOException, TemplateNotFoundException,
			MalformedTemplateNameException, ParseException, TemplateException {
		HashMap<String, Object> Map = new HashMap<>();

		Map.put("classname", classname);
		Map.put("userMap", dateMap);
		Map.put("packagename", packagename);
		URL resource2 = FreemarkUtil.class.getClassLoader().getResource(".");
		String replace = resource2.getFile().replace("bin", "src");
		replace += packagename.replace(".", "/");
		replace += "/" + classname + ".java";
		System.out.println(replace);

		File file = new File(replace);// 处理完成后输出路径
		URL resource = JDBCOOPUtils.class.getResource(".");// 获取JDBCOOPTest的当前路径
		File path = new File(resource.toURI());
		String templeteName = "entry.tpl";// freemark模板名称
		toFile(Map, file, path, templeteName);

	}

	public static void toFile(Map<String, Object> Map, File file, File path,
			String templeteName) throws IOException, URISyntaxException,
			TemplateNotFoundException, MalformedTemplateNameException,
			ParseException, TemplateException {
		/*
		 * 用freemark把结果写出到文件
		 */
		// 1.初始化配置器
		Configuration configuration = new Configuration(
				Configuration.VERSION_2_3_26);
		// 2.设置 模板加载器
		configuration.setDefaultEncoding("utf-8");
		configuration.setDirectoryForTemplateLoading(path);
		// 3.加载模板
		Template template = configuration.getTemplate(templeteName);
		// 4.合成模板
		FileWriter fileWriter = new FileWriter(file);
		template.process(Map, fileWriter);
	}

	public static String getString(Map<String, Object> Map, String templete)
			throws IOException, URISyntaxException, TemplateNotFoundException,
			MalformedTemplateNameException, ParseException, TemplateException {
		/*
		 * 用freemark把结果输出到String
		 */
		// 1.初始化配置器
		Configuration configuration = new Configuration(
				Configuration.VERSION_2_3_26);
		// 2.设置 模板加载器
		configuration.setDefaultEncoding("utf-8");
		// 3.加载模板
		StringTemplateLoader templateLoader = new StringTemplateLoader();
		templateLoader.putTemplate("aaa", templete);
		configuration.setTemplateLoader(templateLoader);
		Template template = configuration.getTemplate("aaa");
		// 4.合成模板
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// 内存数组流
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
				byteArrayOutputStream);
		template.process(Map, outputStreamWriter);
		byte[] byteArray = byteArrayOutputStream.toByteArray();
		return new String(byteArray);
	}
}
